package com.beagle.njddapp.ui.presenter;

import com.beagle.njddapp.base.ApiService;
import com.beagle.njddapp.base.BasePresenter;
import com.beagle.njddapp.base.OilApiService;
import com.beagle.njddapp.base.RetrofitHttp;
import com.beagle.njddapp.bean.LocationBean;
import com.beagle.njddapp.bean.oil.OilHomeBean;
import com.beagle.njddapp.core.LocationManager;
import com.beagle.njddapp.net.BaseOilHttpListResponse;
import com.beagle.njddapp.net.BaseOilHttpResponse;
import com.beagle.njddapp.net.BaseStringResponseBean;
import com.beagle.njddapp.net.HttpResponseObserver;
import com.beagle.njddapp.net.RxSchedulers;
import com.beagle.njddapp.ui.view.OilHomeView;
import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.LinkedHashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.RequestBody;

/**
 * @author hxx
 * @date 2021-03-02 22:33
 */
public class OilHomePresenter extends BasePresenter<OilHomeView> {
    private OilApiService oilApiService;
    private ApiService apiService;

    public OilHomePresenter() {
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        oilApiService = retrofit.create(OilApiService.class);
        apiService = retrofit.create(ApiService.class);

    }


    public void confirmed(int orderId) {

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), "{}");

        oilApiService.JieDan(orderId + "", body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse>() {
            @Override
            public void success(BaseOilHttpResponse oilHomeBeanBaseOilHttpResponse) {
                if (oilHomeBeanBaseOilHttpResponse != null) {
                    getView().showJiedan(orderId);
                } else {
                    getView().onError("");

                }
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }

    public void arrived(int orderId) {

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), "{}");

        oilApiService.arrived(orderId + "", body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse>() {
            @Override
            public void success(BaseOilHttpResponse oilHomeBeanBaseOilHttpResponse) {
                if (oilHomeBeanBaseOilHttpResponse != null) {
                    getView().showArrived();
                } else {
                    getView().onError("");

                }
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }

    public void submit(int orderId,String count) {

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), "{}");

        oilApiService.submit(orderId + "",Float.parseFloat(count), body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse>() {
            @Override
            public void success(BaseOilHttpResponse oilHomeBeanBaseOilHttpResponse) {
                if (oilHomeBeanBaseOilHttpResponse != null) {
                    getView().showSubmit();
                } else {
                    getView().onError("");

                }
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }

    public void getOrderDetail(int orderId) {

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), "{}");

        oilApiService.getOilDetail(orderId + "", body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse<OilHomeBean>>() {
            @Override
            public void success(BaseOilHttpResponse<OilHomeBean> oilHomeBeanBaseOilHttpResponse) {
                if (oilHomeBeanBaseOilHttpResponse != null) {
                    getView().showOrder(oilHomeBeanBaseOilHttpResponse.result);
                } else {
                    getView().onError("");

                }
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }


    public void confirmOrderPayment(OilHomeBean oilHomeBean) {


        Gson gson=new Gson();
        HashMap<String,String> paramsMap=new LinkedHashMap<>();
        paramsMap.put("type","fuelConfirmOrderPayment");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        paramsMap.clear();
        apiService.requestFormToken(body).compose(RxSchedulers.compose()).safeSubscribe(new Observer<BaseStringResponseBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseStringResponseBean baseStringResponseBean) {
                if (baseStringResponseBean.getResult()!=null){
                    Gson gson=new Gson();
                    HashMap<String,String> paramsMap=new LinkedHashMap<>();
                    paramsMap.put("paymentType","5");
                    paramsMap.put("amount",oilHomeBean.getTotalAmount());
                    paramsMap.put("meter",oilHomeBean.getExpectMeter()+"");
                    paramsMap.put("formToken",baseStringResponseBean.getResult());

                    String strEntity = gson.toJson(paramsMap);
                    RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
                    paramsMap.clear();
                    oilApiService.confirmOrderPayment(oilHomeBean.getOrderId() + "", body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse>() {
                        @Override
                        public void success(BaseOilHttpResponse oilHomeBeanBaseOilHttpResponse) {
                            if (oilHomeBeanBaseOilHttpResponse != null) {
                                getView().shoukuan();
                            } else {
                                getView().onError("");

                            }
                        }

                        @Override
                        public void error(int code, String message) {
                            getView().onError(message);
                        }
                    });
                }
            }

            @Override
            public void onError(Throwable e) {
                getView().onError("服务异常");

            }

            @Override
            public void onComplete() {

            }

        });




    }

}
